from rest_framework import serializers
from user.models import User, Topic, Comment
import base64

class UserSerializer(serializers.ModelSerializer):
    # 定义收藏的话题和关注的用户字段
    collected_topics = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    concerned_users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = User
        fields = '__all__'

class UserSerializer2(serializers.ModelSerializer):
    collected_topics = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    concerned_users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = User
        fields = ['id', 'email', 'age', 'name','tel','address','birthday', 'username', 'webname', 'collected_topics', 'concerned_users']


class TopicSerializer(serializers.ModelSerializer):
    publisher_name = serializers.SerializerMethodField()

    def get_publisher_name(self, obj):
        return obj.publisher.webname
    class Meta:
        model = Topic
        fields = ['id', 'title', 'content', 'publish_time', 'topic_type', 'publisher', 'likes_number','dislikes_number', 'is_featured', 'comment_count', 'publisher_name']

class TopicSerializer_2(serializers.ModelSerializer):
    class Meta:
        model = Topic
        fields = ['id', 'title']


class HotSearchSerializer(serializers.Serializer):
    keyword = serializers.CharField(max_length=255)  # 假设关键词是字符串类型
    score = serializers.FloatField()  # 分数是浮点类型


# class TopiccommentSerializer(serializers.ModelSerializer):
#     user = UserSerializer()
#     class Meta:
#         model = Comment
#         fields = '__all__'


class TopiccommentSerializer(serializers.ModelSerializer):
    topic_publisher = serializers.SerializerMethodField()

    def get_topic_publisher(self, obj):
        return obj.topic.publisher.webname

    class Meta:
        model = Comment
        fields = ['id', 'user',  'topic', 'content', 'publish_time', 'topic_publisher']
